import Taro from "@tarojs/taro";
import { EnumMemberState } from "@/apis/endpoint/model";
import { postMyChangejobstatus } from "@/apis/endpoint/my";
import { jobStatusOptions } from "@/pages/me/enum-options";
import { useState } from "react";

interface StateGetter {
  (): { state: EnumMemberState }
}

interface RefreshFn {
  (nextStatus: EnumMemberState, nextStatusOption: IOption<EnumMemberState>): void
}

export function useJobStatus(getState: StateGetter, refreshFn: RefreshFn) {
  const [jobStatusOpen, setJobStatusOpen] = useState(false)
  function toggleJobStatus(open: boolean) {
    setJobStatusOpen(open)
  }
  async function handleJobStatusChosen(option: IOption<EnumMemberState>) {
    if (option.value === getState().state) {
      toggleJobStatus(false)
      return
    }
    await postMyChangejobstatus(option.value).send()
    Taro.showToast({
      title: "设置成功",
      icon: "success"
    })
    toggleJobStatus(false)
    refreshFn(option.value, option)
  }
  return {
    jobStatusOptions,
    jobStatusOpen,
    toggleJobStatus,
    handleJobStatusChosen
  }
}
